#!/bin/bash
set -e
PROCESS_NAME=$1


SH_DIR=$(dirname $0)
WS_DIR="${SH_DIR}/.."
DATE_TEXT=`date +%Y%m%d_%H%M%S`

LOG_FILE="${WS_DIR}/log/${DATE_TEXT}_${PROCESS_NAME}_sysinfo.log"

echo "${DATE_TEXT} ${PROCESS_NAME}" >> ${LOG_FILE} 
cat /proc/cpuinfo | grep -E 'model name' >> ${LOG_FILE}
cat /proc/meminfo | grep -E 'MemTotal'  >> ${LOG_FILE}
echo >> ${LOG_FILE}

# 显卡信息
nvidia-smi -a | grep -E 'Product Name|Driver Version|CUDA Version' >> ${LOG_FILE}
echo >> ${LOG_FILE}


echo "CPU|MEM|GPU|GMEM" >> ${LOG_FILE}
echo >> ${LOG_FILE}


while true
do
	PID=$(ps -e | grep ${PROCESS_NAME} | grep -v 'grep' | awk '{print $1;}')
	sys_info=`top -n 1 -p ${PID} | tail -3 | head -1 | awk '{cpu=NF-4} {mem=NF-3} {printf("%s%% %s%%",$cpu,$mem)}'`
	gpu_info=`nvidia-smi | head -10 | tail -1 | awk '{gmem=NF-6} {gpu=NF-2} {print $gpu,$gmem}'`
	if [ -z "${sys_info}" ]
	then
		echo "\$sys_info is empty"
		break
	fi
	echo "$(date +%Y%m%d_%H%M%S) ${sys_info} ${gpu_info}" | tee -a ${LOG_FILE}
	sleep 1
done
